#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
  pid_t ret = fork();
  sleep(1);

  if(ret > 0)
  {
    printf("父进程正在等待子进程死亡\n");
    int status = 0;
    pid_t rec = waitpid(ret, &status, 0);
    if(rec == ret)
    {
      if((status & 0x7F) == 0)
      {
        printf("正常退出其退出码为: %d\n", (status >> 8) & 0xFF);
      }
      else{
        printf("异常退出，退出信号是:%d\n",status&0x7f);
      }
      exit(0);
    }
    else{
      printf("等待失败\n");
      exit(0);
    }
  }
  else if(ret == 0)
  {
    int count = 1;
    while(count <= 3)
    {
      printf("子进程[%d]已经运行了%d秒\n",getpid(),count);
      count++;
      sleep(1);
    }
    exit(0);
  }
  else 
    printf("进程创建失败\n");

  return 0;
}
